/*
* @file AccountAssets/openposition.js
* 持仓列表
*/

import React from 'react';
import { Table } from 'antd';
import moment from 'moment';
import fx from '../utils/fx';
import dicts from '../dicts';

const columns = [
	{
		title: '股票代码',
		dataIndex: 'symbol',
	},
	{
		title: '类型',
		dataIndex: 'tradeType',
		render: (text, row) => {
			let color = 'green';
			if (text == "Buy") {
				color = 'red';
			}
			return (<span style={{ color: color}}>{dicts.tradeType[text]}</span>);
		},
	},
	{
		title: '价格',
		dataIndex: 'price',
		render: text => fx.formatNumber(text, 2),
	},
	{
		title: '交易数量',
		dataIndex: 'quantity',
	},
	{
		title: '费用',
		dataIndex: 'fee',
		render: text => fx.formatNumber(text, 2),
	},
	{
		title: '浮动盈亏',
		render: (text, row) => {
			const { tradeType, price, currentPrice, quantity, fee } = row;
			if (tradeType == 'Buy') {
				return fx.colorMoney((currentPrice - price) * quantity - fee);
			} else {
				return fx.colorMoney((price - currentPrice) * quantity - fee);
			}
		},
	},
	{
		title: '建仓时间',
		dataIndex: 'timestamp',
		render: text => moment(new Date(text)).format('YYYY-MM-DD HH:mm'),
	},
];

export default ({ list }) => (
	<dl>
		<dt>持仓列表</dt>
		<dd>
			<Table
				size="middle"
				rowKey={row => (row.price + row.quantity)}
				dataSource={list}
				columns={columns}
			/>
		</dd>
	</dl>);
